package com.tarena.csmall.front.web.repository;

import cn.tedu.mall.common.exception.CoolSharkServiceException;
import cn.tedu.mall.common.restful.ResponseCode;
import cn.tedu.mall.pojo.front.dto.ReduceSkuDTO;
import cn.tedu.mall.pojo.product.vo.SkuStandardVO;
import com.tarena.csmall.front.web.mapper.SkuMapper;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class SkuRepositoryImpl implements SkuRepository{
    @Autowired
    private SkuMapper skuMapper;
    @Override public List<SkuStandardVO> getSkus(Long id) {
        return skuMapper.selectSkusBySpuId(id);
    }

    @Override public List<SkuStandardVO> batchGetSkus(List<Long> ids) {
        return skuMapper.selectSkusByIds(ids);
    }

    @Override public void reduceSotck(List<ReduceSkuDTO> dtos) {
        //update pms_sku set stock=stock-#{quantity} where id=#{skuId} and stock>=#{quantity}
//        int rows=skuMapper.updateStocks(dtos);
//        if(rows!=dtos.size()){
//            throw new CoolSharkServiceException(ResponseCode.BAD_REQUEST,"库存不足");
//        }
        for (ReduceSkuDTO dto : dtos) {
            int rows=skuMapper.updateStock(dto);
            if(rows!=1){
                throw new CoolSharkServiceException(ResponseCode.BAD_REQUEST,"库存不足");
            }
        }
    }

    @Override public void reduceLogRecord(List<ReduceSkuDTO> dtos) {
        //如果调用到这个方法,说明前面的库存减少成功
        skuMapper.insertSkuStockLogs(dtos);
    }
}
